package com.ruoyi.combat.util;

import cn.hutool.json.JSONUtil;
import com.ruoyi.combat.domain.userlocation.CombatRelation;
import com.ruoyi.combat.domain.userlocation.CombatUser;
import com.ruoyi.combat.service.userlocation.ICombatRelationService;
import com.ruoyi.combat.service.userlocation.ICombatUserService;
import com.ruoyi.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserLocationTask implements Runnable{

    private CombatUser combatUser;
    private ICombatUserService combatUserService;
    private MethodUseUtils methodUseUtils;
    private ICombatRelationService combatRelationService;

    public UserLocationTask(CombatUser combatUser, ICombatUserService combatUserService,ICombatRelationService combatRelationService, MethodUseUtils methodUseUtils) {
        this.combatUser = combatUser;
        this.combatUserService = combatUserService;
        this.combatRelationService = combatRelationService;
        this.methodUseUtils = methodUseUtils;
    }

    @Override
    public void run() {

        try {
            Thread.sleep(120000L);
            String userId = String.valueOf(combatUser.getId());
            String jsonStr = buildRelation(userId);

            Map<String, Object> stringObjectMap = methodUseUtils.methodUseLogs(50L, jsonStr);
            //
            if (stringObjectMap.get("results") != null) {
                CombatUser combatUser1 = combatUserService.selectCombatUserByUserId(userId);
                String results = JSONUtil.toJsonStr(stringObjectMap.get("results"));

                CombatUser combatUser = new CombatUser();
                combatUser.setId(combatUser1.getId());
                combatUser.setAfterAddress(results);
                combatUserService.updateCombatUser(combatUser);

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    public String buildRelation(String userId){
        Map<String, Object> result = new HashMap<>();
        Map<String, Map> userMap = new HashMap<>();
        List<CombatUser> combatUsers = combatUserService.selectCombatUserList(new CombatUser());
        for (CombatUser user : combatUsers) {
            Map<String, String> data = new HashMap();
            data.put("name", user.getUserNickname());
            data.put("city", user.getUserHomeAddr());
            userMap.put(user.getUserId(), data);
        }
        CombatRelation combatRelation = new CombatRelation();
        combatRelation.setUserid(userId);
        List<CombatRelation> combatRelations = combatRelationService.selectCombatRelationList(combatRelation);

        List<Map<String, String>> list = new ArrayList<>();
        List<Map<String, String>> fake_user = new ArrayList<>();
        for (CombatRelation relation : combatRelations) {
            Map<String, String> data = new HashMap<>();
            String userid = relation.getUserid();
            String username = relation.getUsername();
            String userCity = relation.getUserCity();
            String city1 = CityUtil.getCity(userCity);
            if (StringUtils.isEmpty(city1)) {
                userCity = "郑州";
            }

            String targetid = relation.getTargetid();
            String targetName = relation.getTargetName();
            String targetCity = relation.getTargetCity();
            String city2 = CityUtil.getCity(targetCity);
            if (StringUtils.isEmpty(city2)) {
                targetCity = "郑州";
            }
            data.put("toId", targetid);
            data.put("toName", targetName);
            data.put("toCity", targetCity);
            data.put("fromId", userid);
            data.put("fromName", username);
            data.put("fromCity", userCity);
            data.put("relation", "关注");
            list.add(data);
            if (userMap.get(userId) == null) {
                Map<String, String> data1 = new HashMap<>();
                data1.put("userid", userid);
                data1.put("name", username);
                data1.put("city", userCity);
                fake_user.add(data1);
            }
            if (userMap.get(targetid) == null) {
                Map<String, String> data1 = new HashMap<>();
                data1.put("userid", targetid);
                data1.put("name", targetName);
                data1.put("city", targetCity);
                fake_user.add(data1);
            }
        }
        result.put("target_userid", userId);
        result.put("data", list);
        result.put("fake_user", fake_user);
        String jsonStr = JSONUtil.toJsonStr(result);
        return jsonStr;
    }


}
